package com.java2020.online_mall.utils;

import com.alibaba.druid.util.StringUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @PackgeName: com.java2020.online_mall.utils
 * @Author: MNHK
 * Date: 2020/4/28 23:16
 * @Version:1.8
 */

public class HttpUtils {
    public static String getRequestBody(HttpServletRequest request) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes,0,len);
        }
        return outputStream.toString("utf-8");
    }

    public static boolean isNumber(String id) {
        try {
            if (!StringUtils.isEmpty(id)) {
                Integer.parseInt(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
